-- Lua Battle System - 状态效果管理

local Logger = require("utils.logger")
local BattleConfig = require("config.battle_config")

local StatusEffects = {}

-- 状态效果基类
local StatusEffect = {
    id = "",
    name = "",
    description = "",
    type = "", -- buff, debuff, neutral
    duration = 0,
    max_duration = BattleConfig.status_effects.max_duration,
    stacks = 1,
    max_stacks = BattleConfig.status_effects.max_stack_limit,
    stackable = false,
    tick_interval = BattleConfig.status_effects.tick_interval,
    tick_count = 0
}

-- 状态效果构造函数
function StatusEffect:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    return o
end

-- 应用状态效果
function StatusEffect:on_apply(character)
    Logger.debug(character.name .. " 获得了 " .. self.name .. " 状态效果")
end

-- 移除状态效果
function StatusEffect:on_remove(character)
    Logger.debug(character.name .. " 移除了 " .. self.name .. " 状态效果")
end

-- 更新状态效果
function StatusEffect:update(character)
    self.tick_count = self.tick_count + 1
    if self.tick_count >= self.tick_interval then
        self:on_tick(character)
        self.tick_count = 0
    end
    self.duration = self.duration - 1
end

-- 状态效果触发
function StatusEffect:on_tick(character)
    -- 默认空实现，子类重写
end

-- 状态效果堆叠
function StatusEffect:stack(other_effect)
    if not self.stackable then
        return false
    end
    
    if self.id ~= other_effect.id then
        return false
    end
    
    local new_stacks = self.stacks + other_effect.stacks
    if new_stacks > self.max_stacks then
        new_stacks = self.max_stacks
    end
    
    self.stacks = new_stacks
    self.duration = math.max(self.duration, other_effect.duration)
    return true
end

-- 修改属性
function StatusEffect:modify_stats(stats)
    -- 默认不修改属性，子类重写
    return stats
end

-- 获取状态效果状态
function StatusEffect:get_state()
    return {
        id = self.id,
        name = self.name,
        duration = self.duration,
        stacks = self.stacks
    }
end

-- 具体状态效果实现

-- 燃烧效果
local BurnEffect = StatusEffect:new({
    id = "burn",
    name = "燃烧",
    description = "每回合受到伤害",
    type = "debuff",
    duration = BattleConfig.status_effects.burn.duration,
    stackable = BattleConfig.status_effects.burn.stackable,
    damage_per_tick = BattleConfig.status_effects.burn.damage_per_tick
})

function BurnEffect:on_tick(character)
    local damage = self.damage_per_tick * self.stacks
    character:take_damage(damage)
    Logger.info(character.name .. " 受到燃烧伤害: " .. damage)
end

-- 冰冻效果
local FreezeEffect = StatusEffect:new({
    id = "freeze",
    name = "冰冻",
    description = "无法行动",
    type = "debuff",
    duration = BattleConfig.status_effects.freeze.duration,
    stackable = BattleConfig.status_effects.freeze.stackable,
    chance_to_break = BattleConfig.status_effects.freeze.chance_to_break
})

function FreezeEffect:modify_stats(stats)
    stats.speed = 0 -- 冰冻状态下速度为0，无法行动
    return stats
end

function FreezeEffect:on_apply(character)
    StatusEffect.on_apply(self, character)
    Logger.info(character.name .. " 被冰冻了，无法行动")
end

-- 中毒效果
local PoisonEffect = StatusEffect:new({
    id = "poison",
    name = "中毒",
    description = "每回合受到持续伤害",
    type = "debuff",
    duration = BattleConfig.status_effects.poison.duration,
    stackable = BattleConfig.status_effects.poison.stackable,
    damage_per_tick = BattleConfig.status_effects.poison.damage_per_tick
})

function PoisonEffect:on_tick(character)
    local damage = self.damage_per_tick * self.stacks
    character:take_damage(damage)
    Logger.info(character.name .. " 受到中毒伤害: " .. damage)
end

-- 攻击提升效果
local AttackBuffEffect = StatusEffect:new({
    id = "attack_buff",
    name = "攻击提升",
    description = "攻击力提升",
    type = "buff",
    duration = BattleConfig.status_effects.buff.attack_buff.duration,
    multiplier = BattleConfig.status_effects.buff.attack_buff.multiplier
})

function AttackBuffEffect:modify_stats(stats)
    stats.attack = stats.attack * self.multiplier
    return stats
end

function AttackBuffEffect:on_apply(character)
    StatusEffect.on_apply(self, character)
    Logger.info(character.name .. " 的攻击力提升了 " .. ((self.multiplier - 1) * 100) .. "%")
end

-- 防御提升效果
local DefenseBuffEffect = StatusEffect:new({
    id = "defense_buff",
    name = "防御提升",
    description = "防御力提升",
    type = "buff",
    duration = BattleConfig.status_effects.buff.defense_buff.duration,
    multiplier = BattleConfig.status_effects.buff.defense_buff.multiplier
})

function DefenseBuffEffect:modify_stats(stats)
    stats.defense = stats.defense * self.multiplier
    return stats
end

function DefenseBuffEffect:on_apply(character)
    StatusEffect.on_apply(self, character)
    Logger.info(character.name .. " 的防御力提升了 " .. ((self.multiplier - 1) * 100) .. "%")
end

-- 速度提升效果
local SpeedBuffEffect = StatusEffect:new({
    id = "speed_buff",
    name = "速度提升",
    description = "速度提升",
    type = "buff",
    duration = BattleConfig.status_effects.buff.speed_buff.duration,
    multiplier = BattleConfig.status_effects.buff.speed_buff.multiplier
})

function SpeedBuffEffect:modify_stats(stats)
    stats.speed = stats.speed * self.multiplier
    return stats
end

function SpeedBuffEffect:on_apply(character)
    StatusEffect.on_apply(self, character)
    Logger.info(character.name .. " 的速度提升了 " .. ((self.multiplier - 1) * 100) .. "%")
end

-- 状态效果工厂
function StatusEffects.create_effect(effect_id, params)
    params = params or {}
    
    local effect_map = {
        burn = BurnEffect,
        freeze = FreezeEffect,
        poison = PoisonEffect,
        attack_buff = AttackBuffEffect,
        defense_buff = DefenseBuffEffect,
        speed_buff = SpeedBuffEffect
    }
    
    local effect_class = effect_map[effect_id]
    if not effect_class then
        Logger.error("未知的状态效果: " .. effect_id)
        return nil
    end
    
    local effect = effect_class:new(params)
    return effect
end

-- 导出模块
return StatusEffects